<template>
  <div class="todo-footer" v-show="total">
    <label>
      <input type="checkbox" :checked="isAll" @change="checkAll"/>
      <!-- <input type="checkbox" :checked="isAll" @change="checkAll"/> -->
    </label>
    <span> <span>已完成{{downTotal}}</span> / 全部{{total}} </span>
    <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name: "TodoFooter",
  data() {
    return {
      checkFlag:''
    }
  },
  props:['todoArr','checkAllTodo','clearAllTodo'],
  computed:{
    //使用笨方法
    // downTotal(){
    //   let i = 0
    //   this.todoArr.forEach(todo => {
    //     if(todo.done) i++
    //   });
    //   return i
    // }
    //使用数组的reduce方法进行筛选
    // downTotal(){
      // return this.todoArr.reduce((pre,todo)=>{
        // if (todo.done) {
            // pre++
        // }
        // return pre
      // },0)
    // }
    total(){
      return this.todoArr.length
    },
    downTotal(){
      // return this.todoArr.reduce((pre,todo)=>{ return pre+ (todo.done ? 1:0) },0)
      const x = this.todoArr.reduce((pre,todo)=> pre+ (todo.done ? 1:0) ,0)  //根据上一行，进一步简写
      if(x === this.todoArr.length){
        this.checkFlag=true
      }
      return x
    },
    isAll(){
      return this.total === this.downTotal  && this.total>0    //返回boolean类型，判断是否已经全选中
    }
  },
  methods: {
    checkAll(e){
      // console.log(e.target.checked);
      this.checkAllTodo(e.target.checked)
    },
    clearAll(){
      this.clearAllTodo()
    }
  },

};
</script>

<style scoped>
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>